package task2;

import java.util.Scanner;

public class Chess {
    private String[][] board;
    /**
     * 初始化棋盘
     */
    {
        board = new String[17][17];
        char[] arr = "0123456789abcdef".toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                if (0 == i) {
                    if (0 == j) {
                        board[i][j] = "";
                    } else {
                        board[i][j] = String.valueOf(arr[j - 1]);
                    }
                } else {
                    if (0 == j) {
                        board[i][j] = String.valueOf(arr[i - 1]);
                    } else {
                        board[i][j] = "+";
                    }
                }
            }
        }
    }

    /**
     * 打印棋盘
     */
    public void show() {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                System.out.print(board[i][j] + '\t');
                if (board[i].length - 1 == j) {
                    System.out.print("\n");
                }
            }
        }
    }

    /**
     * @param player 玩家编号
     * @param x      行数
     * @param y      列数
     * @return 是否获胜，比赛是否结束
     */
    public boolean play(String player, String x, String y) {
        int xi = Integer.valueOf(x, 16);
        int yi = Integer.valueOf(y, 16);
        board[xi + 1][yi + 1] = player;
        this.show();
        if (ifWin(player, xi + 1, yi + 1)) {
            System.out.println("游戏结束，" + player + "获胜!");
            return true;
        }
        return false;
    }

    /**
     * 检验输入坐标是否合法
     *
     * @return 是否合法
     */
    public boolean check(String x, String y) {
        // 判断输入坐标长度是否合法
        if (x.length() != 1 || y.length() != 1) {
            System.out.println("请输入合法坐标");
            return false;
        }
        // 判断坐标是否在棋盘上
        char xc = x.toCharArray()[0];
        char yc = y.toCharArray()[0];
        boolean xb = false;
        boolean yb = false;
        for (char c : "0123456789abcdef".toCharArray()) {
            if (c == xc) {
                xb = true;
                break;
            }
        }
        for (char c : "0123456789abcdef".toCharArray()) {
            if (c == yc) {
                yb = true;
                break;
            }
        }
        if (!(xb && yb)) {
            System.out.println("请输入合法坐标");
            return false;
        }

        // 判断输入位置是否有棋子
        int xi = Integer.valueOf(x, 16);
        int yi = Integer.valueOf(y, 16);
        if (board[xi + 1][yi + 1] != "+") {
            System.out.println("该位置已有棋子");
            return false;
        }
        return true;
    }

    /**
     * 判断是否获胜
     *
     * @param player 下棋方
     * @param xi     行坐标
     * @param yi     列坐标
     * @return 是否获胜
     */
    private boolean ifWin(String player, int xi, int yi) {
        // 纵向
        int i = xi;
        int j = yi;
        int count = 0;
        // 向上累加
        while (i > 0 && player.equals(board[i][j])) {
            i--;
            count++;
        }
        // 向下累加
        i = xi + 1;
        while (i < 17 && player.equals(board[i][j])) {
            i++;
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // 横向
        i = xi;
        count = 0;
        // 向左累加
        while (j > 0 && player.equals(board[i][j])) {
            j--;
            count++;
        }
        // 向右累加
        j = yi + 1;
        while (j < 17 && player.equals(board[i][j])) {
            j++;
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // 左上角到右下角
        i = xi;
        j = yi;
        count = 0;
        while (i > 0 && j > 0 && player.equals(board[i][j])) {
            i--;
            j--;
            count++;
        }
        i = xi + 1;
        j = yi + 1;
        while (i < 17 && j < 17 && player.equals(board[i][j])) {
            i++;
            j++;
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // 右上角到左下角
        i = xi;
        j = yi;
        count = 0;
        while (i > 0 && j < 17 && player.equals(board[i][j])) {
            i--;
            j++;
            count++;
        }
        i = xi + 1;
        j = yi - 1;
        while (i < 17 && j > 0 && player.equals(board[i][j])) {
            i++;
            j--;
            count++;
        }
        return count >= 5;
    }


    public static void main(String[] args) {
        Chess chess = new Chess();
        chess.show();
        System.out.println("游戏开始");
        boolean flag = false;
        int p = 1;
        while (!flag) {
            boolean checkFlag = false;
            while (!checkFlag) {
                System.out.println("请" + (p == 1 ? "A" : "B") + "下棋");
                System.out.println("请输入落子坐标");
                Scanner scanner = new Scanner(System.in);
                String x = scanner.next();
                String y = scanner.next();
                if (chess.check(x, y)) {
                    checkFlag = true;
                    flag = chess.play((p == 1 ? "A" : "B"), x, y);
                    p *= -1;
                }
            }
        }
    }
}
